package com.bluesky.android.webservice;



import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;
import android.util.Xml;


import com.bluesky.android.webservice.HandleXMLFactory.HandlerType;

public class BaseFetcher{
	
	private String mangaServiceNamespace = "http://tempuri.org/";
    private String mangaServiceURL = "http://demo.zoha.com.au/MangaService.svc";
    private String soapActionBase = "http://tempuri.org/IMangaService/";
    protected  SoapSerializationEnvelope envelope;
    protected SoapObject response;
    protected SoapObject request;   
    protected HttpTransportSE httpRequest;
    protected BaseHandler parse;
    public BaseFetcher(HandlerType typeParse) {
    	this.parse = HandleXMLFactory.getHandler(typeParse);
    	request = new SoapObject(mangaServiceNamespace, this.parse.getServiceFunctionForHandle());
    	envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;        
        envelope.setOutputSoapObject(request);
        soapActionBase = soapActionBase + this.parse.getServiceFunctionForHandle();
    }
    
    public void addPropertyInfo(String name, Object value, Object type){
    	PropertyInfo pInfo = new PropertyInfo();
		pInfo.setName(name);
		pInfo.setValue(value);
		pInfo.setType(type);		
    	this.request.addProperty(pInfo);
    }
    
    public Object fetch(){
    	httpRequest = new HttpTransportSE(mangaServiceURL);
    	Object resultValue = null;
    	try{
    		httpRequest.call(soapActionBase, envelope);
    		response = (SoapObject) envelope.getResponse();
    		
    		resultValue = this.parse.getResult(response);
    		
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    		
    	}    	
    	return resultValue;
    }
}
